<template>
  <div class="pagination">
    <!-- 上半部 -->
    <button
      v-show="continuesStarAndEnd.start > 1"
      @click="$emit('getPageNo', 1)"
      :class="{ active: pageNo == 1 }"
    >
      1
    </button>
    <button :disabled="pageNo == 1" @click="$emit('getPageNo', pageNo - 1)">
      上一页
    </button>

    <button v-if="continuesStarAndEnd.start > 2">···</button>
    <!-- 中间continues部分 -->
    <button
      :class="{ active: pageNo == page }"
      v-for="(page, index) in continuesStarAndEnd.end"
      :key="index"
      v-show="page >= continuesStarAndEnd.start"
      @click="$emit('getPageNo', page)"
    >
      {{ page }}
    </button>
    <!-- 最后面大数分页部分 -->

    <button v-if="continuesStarAndEnd.end < totalPage - 1">···</button>
    <button
      :class="{ active: pageNo == totalPage }"
      v-show="continuesStarAndEnd.end < totalPage"
      @click="$emit('getPageNo', totalPage)"
    >
      {{ totalPage }}
    </button>
    <button
      @click="$emit('getPageNo', pageNo + 1)"
      :disabled="pageNo == totalPage"
    >
      下一页
    </button>

    <button style="margin-left: 30px">共 {{ Total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["pageNo", "pageSize", "Total", "continues"],
  computed: {
    //一共有多少页的数据
    totalPage() {
      return Math.ceil(this.Total / this.pageSize);
    },
    //continues 的start   和 end
    continuesStarAndEnd() {
      //这里解构一下 需要的值 下面书写的时候就会更加方便 就不需要写this 了
      const { continues, totalPage, pageNo } = this;
      let start = 0;
      let end = 0;
      //当总页数 小于 continues的时候
      if (totalPage < this.continues) {
        start = 1;
        end = this.continues;
      } else {
        //否则让 start 等于 当前页减去 continues处2  切取整
        //同样 end  加上这个取整的值即可
        start = pageNo - parseInt(continues / 2);
        end = pageNo + parseInt(continues / 2);
      }
      //当start  出现0 和负数    end  大于或者等于  总页数
      if (start <= 0) {
        //首1 尾5
        start = 1;
        end = end = this.continues;
      } else if (end >= this.totalPage) {
        //尾最大  首 ：最大值-长度+1
        end = this.totalPage;
        start = this.totalPage - this.continues + 1;
      }
      //计算属性最终返回的值
      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
.active {
  background-color: skyblue;
}
</style>
